import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:provider_demo/model/cart_model.dart';
import 'package:provider_demo/model/vote_model.dart';

import 'next_page.dart';

class HomePageWidget extends StatelessWidget {
  const HomePageWidget({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: EdgeInsets.only(top: 200),
      child: Column(
        children: [
          CounterNumber(),
          CartButton(),
          NextButton(),

          VoteCodeWidget(),
        ],
      ),
    );
  }


}

class CounterNumber extends StatelessWidget {
  const CounterNumber({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Center(
        child: Consumer<CartModel>(
          builder: (context, cart, child){
            return Text("${cart.cnt}",style: TextStyle(fontSize: 20),);
          },
        ),
      ),
    );
  }
}

class CartButton extends StatelessWidget {
  const CartButton({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final cartModel = context.watch<CartModel>();
    return Container(
      child: ElevatedButton(
        onPressed: (){
          cartModel.increment();
        },
        child: Text("增加"),
      ),
    );
  }
}

class VoteCodeWidget extends StatelessWidget {
  const VoteCodeWidget({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final VoteModel voteModel = context.watch();
    return Container(
      child: Column(
        children: [
          VoteContent(),
          Row(
            children: [
              _circleItem("01", voteModel, Color.fromRGBO(249, 104, 8, 5), Color.fromRGBO(254, 245, 231, 5)),
              _circleItem("02", voteModel, Color.fromRGBO(249, 104, 8, 5), Color.fromRGBO(254, 245, 231, 5)),
              _circleItem("03", voteModel, Color.fromRGBO(249, 104, 8, 5), Color.fromRGBO(254, 245, 231, 5)),
            ],
          )
        ],
      ),
    );
  }

  Widget _circleItem(String number,VoteModel voteModel, Color a, Color b){
    bool isSelected = voteModel.codeList.contains(number);
    return Container(
      child: ElevatedButton(
        style: ButtonStyle(
          shape: MaterialStateProperty.all(CircleBorder(
              side: BorderSide(
                  width: 20,
                  style: BorderStyle.none
              )
          )),
          backgroundColor: MaterialStateProperty.all(isSelected?b:a),
          foregroundColor: MaterialStateProperty.all(isSelected?a:b),
        ),
        onPressed: () {
          print("object>>>>${number}");
          voteModel.storeCode(number);
        },
        child: Text(number),
      ),
    );
  }

}

class VoteContent extends StatelessWidget {
  const VoteContent({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final voteModel = context.watch<VoteModel>();
    return Container(
      margin: EdgeInsets.only(top:20,bottom: 10),
      width: 200,
      height: 50,
      child: Text("投票号码：${voteModel.getVoteContent()}", style: TextStyle(fontSize: 20),),
    );
  }
}



class NextButton extends StatelessWidget {
  const NextButton({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final cartModel = context.watch<CartModel>();
    return Container(
      child: ElevatedButton(
        onPressed: (){
          cartModel.increment();
          Navigator.push(context, MaterialPageRoute(builder: (_)=>NextPage()));
        },
        child: Text("+1跳转下页"),
      ),
    );
  }
}


